Imports System.Collections.Generic
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Namespace Helpers
	''' <summary>
	''' tuanmd added: encrypt and decrypt text
	''' </summary>
	Public Class Encryption
		' use an 8 byte key for DES encryption
		Private Shared _key As Byte() = New Byte() {&Hba, &H87, &H9, &Hdc, &Hfe, &H65, _
			&H43, &H21}

		Private Shared _IV As Byte() = New Byte() {&H12, &H34, &H56, &H78, &H90, &Hab, _
			&Hcd, &Hef}

		Public Shared Function Encrypt(data As String) As String
			Dim des As New DESCryptoServiceProvider()

			' convert input to byte array
			Dim input As Byte() = Encoding.UTF8.GetBytes(data)

			Dim ms As New MemoryStream()
			Dim cs As New CryptoStream(ms, des.CreateEncryptor(Encryption._key, Encryption._IV), CryptoStreamMode.Write)

			cs.Write(input, 0, input.Length)
			cs.FlushFinalBlock()

			Return Convert.ToBase64String(ms.ToArray())
		End Function

		Public Shared Function Decrypt(data As String) As String
			Dim input As Byte() = Convert.FromBase64String(data)
			Dim des As New DESCryptoServiceProvider()
			Dim ms As New MemoryStream()
			Dim cs As New CryptoStream(ms, des.CreateDecryptor(Encryption._key, Encryption._IV), CryptoStreamMode.Write)

			cs.Write(input, 0, input.Length)
			cs.FlushFinalBlock()

			Return Encoding.UTF8.GetString(ms.ToArray())
		End Function
	End Class
End Namespace
